"use strict";

const Base = require("../base");
const fixedList = require("./mskblist");
const LANG = require("../../language"); // 插件语言库
const LANG_T = antSword["language"]["toastr"]; // 通用通知提示
class ExploitSuggest extends Base {
  createToolbar(cell) {
    let self = this;
    let toolbar = cell.attachToolbar();
    this.toolbar = toolbar;
    toolbar.attachEvent("onClick", function (id) {
      if (self.precheck() == false) {
        toastr.error(LANG["precheck"]["not_support"], LANG_T["error"]);
        return;
      }
      console.log(id);
      switch (id) {
        case "exploit":
          try {
            if (self.exptype == "auto") {
              self.autoExploit();
            } else {
              self.manualExploit();
            }
          } catch (e) {
            toastr.error(
              e.constructor === Object ? JSON.stringify(e) : e,
              LANG_T["error"]
            );
          }
          break;
        case "auto":
          self.exptype = "auto";
          self.toolbar.enableItem("exploit");
          break;
        case "manual":
          self.exptype = "manual";
          let editor = null;
          // 初始化编辑器
          editor = ace.edit(self.cellA.cell.lastChild);
          editor.$blockScrolling = Infinity;
          editor.setTheme("ace/theme/tomorrow");
          editor.session.setUseWrapMode(true);
          editor.session.setWrapLimitRange(null, null);

          editor.setOptions({
            fontSize: "14px",
            enableBasicAutocompletion: true,
            enableSnippets: true,
            enableLiveAutocompletion: true,
          });
          const inter = setInterval(editor.resize.bind(editor), 200);
          self.win.win.attachEvent("onClose", () => {
            clearInterval(inter);
            return true;
          });
          self.editor2 = editor;
          self.toolbar.enableItem("exploit");
          break;
        default:
          break;
      }
    });
    toolbar.loadStruct(
      '<toolbar><item type="button" id="exploit" text="exploit" img="" /><item type="separator" id="separator" /><item type="buttonSelect" id="method" text="获取方式" ><item type="button" id="auto" text="自动获取" image="" /><item type="button" id="manual" text="手动输入" /></item></toolbar>',
      function () {}
    );
    toolbar.disableItem("exploit");
  }
  manualExploit() {
    let self = this;
    let text = self.editor2.session.getValue();
    let result = self.getMSKB(text);
    self.editor.session.setValue(result);
    self.editor.setReadOnly(true);
    toastr.success(LANG["success"], LANG_T["success"]);
  }

  autoExploit() {
    let self = this;
    self.core = this.top.core;
    let args = this.getArgs();
    let data = this.genPayload(args);
    self.core
      .request(data)
      .then((_ret) => {
        let res = antSword.unxss(_ret["text"], false);
        if (res === "") {
          res = "output is empty.";
        }
        let result = self.getMSKB(res);
        this.editor.session.setValue(result);
        this.editor.setReadOnly(true);
        toastr.success(LANG["success"], LANG_T["success"]);
      })
      .catch((e) => {
        console.log(e);
        toastr.error(e.constructor === Object ? JSON.stringify(e) : e, "Error");
      });
  }
  getMSKB(text) {
    var text = text.toLowerCase();
    var result = "";
    for (let i = 0; i < Object.keys(fixedList).length; i++) {
      var fixedId = fixedList[i][1];
      if (
        text.indexOf("kb") != -1 &&
        text.indexOf(fixedId.toLowerCase()) == -1
      ) {
        // result+="微软编号   补丁编号    描述    影响系统"
        result +=
          "微软编号: " +
          fixedList[i][0] +
          " <=> 补丁编号: " +
          fixedId +
          " <=> 描述: " +
          fixedList[i][2] +
          " <=> 影响系统: " +
          fixedList[i][3] +
          "\n";
      }
    }
    return result;
  }
}
module.exports = ExploitSuggest;
